import Vue from 'vue'
import Router from 'vue-router'
import store from '@/store'
// import cookie from '@/utils/cookie'

Vue.use(Router)

const routes = [{
  path: '*',
  component: () =>
    import('@/view/extension/404')
},
{
  path: '/viewpoint',
  meta: {
    auth: false,
    title: '观点'
  },
  component: () => import('@/view/viewpoint')
},
{
  path: '/',
  name: 'home',
  meta: {
    auth: false,
    title: ''
  },
  component: () => import('@/view/home')
},
// 登录使用
{
  path: '/liveIndex',
  meta: {
    auth: false,
    title: '直播'
  },
  component: () =>
    import('@/view/liveIndex')
},
{
  path: '/marketView',
  meta: {
    auth: false,
    title: '行情'
  },
  component: () =>
    import('@/view/marketView')
},
{
  path: '/us',
  meta: {
    auth: false
  },
  component: () => import('@/view/us'),
  children: [
    {
      path: 'contact',
      meta: {
        auth: false,
        title: '联系我们'
      },
      component: () => import('@/view/us/contact')
    },
    {
      path: 'about',
      meta: {
        auth: false,
        title: '关于我们'
      },
      component: () => import('@/view/us/about')
    }
  ]
},
{
  path: '/apply',
  meta: {
    auth: false,
    title: '申请入驻'
  },
  component: () =>
    import('@/view/apply')
},
{
  path: '/guide',
  meta: {
    auth: false,
    title: '新用户福利'
  },
  component: () =>
    import('@/view/guide')
},
{
  path: '/guide01',
  meta: {
    auth: false,
    title: '新用户福利'
  },
  component: () =>
    import('@/view/guide01')
},
{
  path: '/guide02',
  meta: {
    auth: false,
    title: '新用户福利'
  },
  component: () =>
    import('@/view/guide02')
},
{
  path: '/guide03',
  meta: {
    auth: false,
    title: '新用户福利'
  },
  component: () =>
    import('@/view/guide03')
},
{
  path: '/guide04',
  meta: {
    auth: false,
    title: '新用户福利'
  },
  component: () =>
    import('@/view/guide04')
},
{
  path: '/checkIn',
  meta: {
    auth: true,
    title: '签到'
  },
  component: () =>
    import('@/view/checkIn')
},
{
  path: '/recharge',
  meta: {
    auth: true,
    title: '中亿理投师-充值'
  },
  component: () =>
      import('@/view/recharge')
},
{
  path: '/liveRoom',
  meta: {
    auth: false,
    title: '直播间'
  },
  component: () =>
    import('@/view/liveRoom')
},
{
  path: '/viewpoint',
  meta: {
    auth: false,
    title: '观点'
  },
  component: () =>
    import('@/view/viewpoint')
}, {
  path: '/superior',
  meta: {
    auth: false,
    title: '找高手'
  },
  component: () =>
    import('@/view/superior')
},
{
  name: 'viewpointWrite',
  path: '/viewpointWrite/:id?',
  meta: {
    auth: true,
    title: '观点'
  },
  component: () => import('@/view/viewpointWrite')
},
{
  path: '/dashboardUser',
  meta: {
    auth: true,
    title: '主页'
  },
  redirect: '/dashboardUser/live',
  component: () =>
    import('@/view/dashboardUser/layout'),
  children: [{
    path: 'live',
    meta: {
      auth: false,
      title: '我的观点'
    },
    component: () => import('@/view/dashboardUser/live')
  }, {
    path: 'viewpoint',
    meta: {
      auth: false,
      title: '我的观点'
    },
    component: () => import('@/view/dashboardUser/viewpoint')
  }, {
    path: 'plan',
    meta: {
      auth: false,
      title: '我的计划'
    },
    component: () => import('@/view/dashboardUser/plan')
  }, {
    path: 'qa',
    meta: {
      auth: false,
      title: '我的问答'
    },
    component: () => import('@/view/dashboardUser/qa')
  }, {
    path: 'feedback',
    meta: {
      auth: false,
      title: '意见反馈'
    },
    component: () => import('@/view/dashboardUser/feedback')
  }, {
    path: '/myorder',
    meta: {
      auth: false,
      title: '我的订单'
    },
    component: () => import('@/view/dashboardAuth/order')
  }, {
    path: '/myasset',
    meta: {
      auth: false,
      title: '中亿理投师-我的资产'
    },
    component: () => import('@/view/dashboardUser/asset')
  }, {
    path: '/mycollect',
    redirect: '/mycollect/qa',
    meta: {
      auth: false,
      title: '我的收藏'
    },
    component: () => import('@/view/dashboardUser/mycollect/collect'),
    children: [
      {
        path: 'qa',
        meta: {
          auth: false,
          title: '我的收藏问答'
        },
        component: () => import('@/view/dashboardUser/mycollect/qa')
      },
      {
        path: 'plan',
        meta: {
          auth: false,
          title: '我的收藏计划'
        },
        component: () => import('@/view/dashboardUser/mycollect/plan')
      },
      {
        path: 'viewpoint',
        meta: {
          auth: false,
          title: '我的收藏观点'
        },
        component: () => import('@/view/dashboardUser/mycollect/viewpoint')
      }
    ]
  }]
},
{
  path: '/dashboard',
  meta: {
    auth: false,
    title: '主页'
  },
  redirect: '/liveIndex',
  component: () => import('@/view/dashboard/layout'),
  children: [{
    path: 'viewpoint/:id',
    meta: {
      auth: false,
      title: '*'
    },
    component: () => import('@/view/dashboard/index')
  },
  {
    path: 'live/:id',
    meta: {
      auth: false,
      title: '*'
    },
    component: () => import('@/view/dashboard/live')
  },
  {
    path: 'plan/:id',
    meta: {
      auth: false,
      title: '*'
    },
    component: () => import('@/view/dashboard/plan')
  },
  {
    path: 'qa/:id',
    meta: {
      auth: false,
      title: '*'
    },
    component: () => import('@/view/dashboard/qa')
  }
  ]
},
{
  path: '/dashboardAuth',
  meta: {
    auth: true,
    title: '主页'
  },
  redirect: '/dashboardAuth/viewpoint',
  component: () => import('@/view/dashboardAuth/layout'),
  children: [{
    path: '/dashboardAuth/viewpoint',
    meta: {
      auth: false,
      title: '我的观点'
    },
    component: () => import('@/view/dashboardAuth/index')
  },
  {
    path: 'plan',
    meta: {
      auth: false,
      title: '我的计划'
    },
    component: () => import('@/view/dashboardAuth/plan/planlist')
  },
  {
    path: 'planarticle',
    meta: {
      auth: false,
      title: '我的计划文章'
    },
    component: () => import('@/view/dashboardAuth/plan/articlelist')
  },
  {
    path: 'answer',
    meta: {
      auth: false,
      title: '我的问答'
    },
    component: () => import('@/view/dashboardAuth/answer')
  },
  {
    path: 'order',
    meta: {
      auth: false,
      title: '我的订单'
    },
    component: () => import('@/view/dashboardAuth/order')
  },
  {
    path: 'feedback',
    meta: {
      auth: false,
      title: '意见反馈'
    },
    component: () => import('@/view/dashboardAuth/feedback')
  },
  {
    path: 'mybuyed',
    meta: {
      auth: false,
      title: '已购内容'
    },
    component: () => import('@/view/dashboardAuth/mybuyed')
  },
  {
    path: 'collect',
    redirect: '/dashboardAuth/collect/qa',
    meta: {
      auth: false,
      title: '我的收藏'
    },
    component: () => import('@/view/dashboardUser/mycollect/collect'),
    children: [{
      path: 'qa',
      meta: {
        auth: false,
        title: '我的收藏问答'
      },
      component: () => import('@/view/dashboardUser/mycollect/qa')
    },
    {
      path: 'plan',
      meta: {
        auth: false,
        title: '我的收藏计划'
      },
      component: () => import('@/view/dashboardUser/mycollect/plan')
    },
    {
      path: 'viewpoint',
      meta: {
        auth: false,
        title: '我的收藏观点'
      },
      component: () => import('@/view/dashboardUser/mycollect/viewpoint'),
      children: [
        {
          path: 'qa',
          meta: {
            auth: false,
            title: '我的收藏问答'
          },
          component: () => import('@/view/dashboardUser/mycollect/qa')
        },
        {
          path: 'plan',
          meta: {
            auth: false,
            title: '我的收藏计划'
          },
          component: () => import('@/view/dashboardUser/mycollect/plan')
        },
        {
          path: 'viewpoint',
          meta: {
            auth: false,
            title: '我的收藏观点'
          },
          component: () => import('@/view/dashboardUser/mycollect/viewpoint')
        }
      ]
    }
    ]
  }
  ]
}, {
  path: '/myLive',
  meta: {
    auth: true,
    title: '我的直播'
  },
  component: () =>
    import('@/view/myLive')
}, {
  path: '/wallet',
  meta: {
    auth: true,
    title: '我的钱包'
  },
  component: () =>
    import('@/view/wallet')
}, {
  path: '/myReward',
  meta: {
    auth: true,
    title: '我的打赏'
  },
  component: () =>
    import('@/view/myReward')
}, {
  path: '/setting',
  meta: {
    auth: true,
    title: '资料设置'
  },
  component: () =>
    import('@/view/setting')
}, {
  path: '/settingAuth',
  meta: {
    auth: true,
    title: '资料设置'
  },
  component: () =>
    import('@/view/settingAuth')
}, {
  path: '/articleDetails/:id',
  name: 'articleDetails',
  meta: {
    auth: false,
    title: '*'
  },
  component: () => import('@/view/articleDetails')
}, {
  path: '/tags/:id',
  name: 'tags',
  meta: {
    auth: false,
    title: '标签'
  },
  component: () => import('@/view/tags')
},
{
  path: '/plan',
  meta: {
    auth: false,
    title: '计划'
  },
  component: () =>
    import('@/view/plan/list')
},
{
  path: '/plan/new',
  meta: {
    auth: true,
    title: '创建计划包'
  },
  component: () =>
    import('@/view/dashboardAuth/plan/planinfo')
},
{
  path: '/plan/edit/:id',
  meta: {
    auth: true,
    title: '修改计划包'
  },
  component: () =>
    import('@/view/dashboardAuth/plan/planinfo')
},
{
  path: '/plan/more',
  meta: {
    auth: false,
    title: '计划-最新计划'
  },
  component: () =>
    import('@/view/plan/more')
},
{
  path: '/plan/:id',
  meta: {
    auth: false,
    title: '计划-订阅'
  },
  component: () =>
    import('@/view/plan/detail')
},
{
  path: '/plan/pay/:id',
  name: 'planpay',
  meta: {
    auth: true,
    title: '计划-支付'
  },
  component: () =>
    import('@/view/plan/pay')
},
{
  path: '/planarticle/new',
  meta: {
    auth: true,
    title: '创建计划文章'
  },
  component: () =>
    import('@/view/dashboardAuth/plan/articleinfo')
},
{
  path: '/planarticle/edit/:id',
  meta: {
    auth: true,
    title: '修改计划文章'
  },
  component: () =>
    import('@/view/dashboardAuth/plan/articleinfo')
}, {
  path: '/planarticle/:id',
  meta: {
    auth: false,
    title: '文章详情'
  },
  component: () =>
    import('@/view/plan/articledetail')
}, {
  path: '/qa',
  meta: {
    auth: false,
    title: '问答'
  },
  component: () =>
    import('@/view/questionAnswer/index')
}, {
  path: '/qa/allmasters',
  meta: {
    auth: false,
    title: '提问分类'
  },
  component: () =>
    import('@/view/questionAnswer/allmasters')
}, {
  path: '/qa/:id',
  meta: {
    auth: false,
    title: '*'
  },
  component: () =>
    import('@/view/questionAnswer/detail')
}, {
  path: '/evaluating',
  meta: {
    auth: false,
    title: '风险评测'
  },
  component: () =>
    import('@/view/evaluating/index')
}, {
  path: '/declare',
  meta: {
    auth: false,
    title: '常见问题'
  },
  component: () =>
    import('@/view/declare/index')
}
]

const router = new Router({
  mode: 'history',
  routes,
  scrollBehavior (to, from, savedPosition) {
    return savedPosition || {
      x: 0,
      y: 0
    }
  }
})

router.beforeEach(async (to, from, next) => {
  // 设置document.title
  if (to.meta.title !== '*') {
    document.title = to.meta.title ? to.meta.title + '_中亿理投师' : '中亿理投师-多元化金融咨询服务平台'
  }
  // setTimeout(() => {
  //   if (window.layer) {
  //     window.layer.closeAll()
  //   }
  // }, 1000)
  next()
  // 需要登录
  if (to.matched.some(record => record.meta.auth)) {
    if (!store.state.me.sysUser.userId) {
      await store.dispatch('getUserInfo')
    }
    if (!store.state.me.sysUser.userId) {
      next('/viewpoint')
    } else {
      next()
    }
  }
  next()
  if (!store.state.me.sysUser.userId) {
    await store.dispatch('getUserInfo')
    next()
  }
  next()
  return false
  // }

  // if (cookie.get('device_id')) {
  //   await store.dispatch('getUserInfo')
  //   let me = store.state.me
  //   if (me.sysUser) {
  //     next()
  //   } else {
  //     next('/401')
  //     // 跳转到一个不需要强制登陆的页面，例如首页
  //   }
  // } else {
  //   next('/401')
  //   // 跳转到一个不需要强制登陆的页面，例如首页
  // }
})

export default router
